﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleDatabaseAdapter;
using SQLinq;
using System.Data;

namespace SimpleDatabaseAdapterConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            RunSimpleDataAdapterTest();
        }

        private static void RunSimpleDataAdapterTest()
        {
            const string testBookName = "Book 1";
            const string testBookDescription = "It is wow description!!!!";
            const int testBookPageCount = 100;
            const float testBookPrice = 10.45F;
            const bool testBookIsEnable = true;
            DateTime testBookCreateDate = new DateTime(2012, 5, 15);
            const string testBookAuthor = "Block B.B.";
            
            SQLinq<AllDataItemBooks> query;
            AllDataItemsCollection books;

            //Create new simple data adapter
            SimpleDataAdapter adapter = new SimpleDataAdapter("Data Source=sps2010dev3;Initial Catalog=TestDataBase;Integrated Security=True", "Lists_1", "AllData_1");

            //Create tables
            adapter.Lists.CheckTable();
            adapter.AllData.CheckTable();

            //Check list
            adapter.Lists.CheckList(typeof(AllDataItemBooks));

            //Get list
            ListData list = adapter.Lists.GetList(typeof(AllDataItemBooks));

            //Delete old items
            query = new SQLinq<AllDataItemBooks>();
            AllDataItemsCollection oldItems = adapter.AllData.GetItems<AllDataItemBooks>(query);
            adapter.AllData.DeleteItems(oldItems.Items.Select(x => x.Id).ToList());

            //Add new item
            AllDataItemBooks book = list.GetNewListItem<AllDataItemBooks>();
            book.Name = testBookName;
            book.Description = testBookDescription;
            book.PageCount = testBookPageCount;
            book.Price = testBookPrice;
            book.IsEnable = testBookIsEnable;
            book.Date = testBookCreateDate;
            book.Author = testBookAuthor;

            long newItemId = adapter.AllData.AddItem(book);

            //Check add new item
            query = new SQLinq<AllDataItemBooks>().Where(x => x.Name == testBookName);
            books = adapter.AllData.GetItems<AllDataItemBooks>(query);

            if (books.Count == 0)
            {
                throw new Exception("Not add or select item!!!");
            }
            else
            {
                if (books.Count > 1)
                {
                    throw new Exception("Not single item!!!");
                }
                else
                {
                    AllDataItemBooks curBook = books[0] as AllDataItemBooks;
                    if (curBook.Name != testBookName
                        || curBook.Description != testBookDescription
                        || curBook.PageCount != testBookPageCount
                        || curBook.Price != testBookPrice
                        || curBook.IsEnable != testBookIsEnable
                        || curBook.Date != testBookCreateDate
                        || curBook.Author != testBookAuthor
                        )
                    {
                        throw new Exception("Item data is bad!!!");
                    }
                }
            }

            //Update item
            testBookCreateDate = DateTime.Now;
            book.Id = newItemId;
            book.Date = testBookCreateDate;

            adapter.AllData.UpdateItem(book);

            //Check update item
            query = new SQLinq<AllDataItemBooks>().Where(x => x.Name == testBookName);
            books = adapter.AllData.GetItems<AllDataItemBooks>(query);

            if (books.Count == 0)
            {
                throw new Exception("Item not fount!!!");
            }
            else
            {
                if (books.Count > 1)
                {
                    throw new Exception("Not single item!!!");
                }
                else
                {
                    AllDataItemBooks curBook = books[0] as AllDataItemBooks;
                    if (curBook.Date.Second != testBookCreateDate.Second)
                    {
                        throw new Exception("Update Item data is bad!!!");
                    }
                }
            }

            //Delete item
            adapter.AllData.DeleteItem(book.Id);

            //Check delete item
            query = new SQLinq<AllDataItemBooks>().Where(x => x.Name == testBookName);
            books = adapter.AllData.GetItems<AllDataItemBooks>(query);

            if (books.Count != 0)
            {
                throw new Exception("Item not delete!!!");
            }

            //End test
            Console.WriteLine("Test successful.");
        }
    }

    [ListName("Books")]
    public class AllDataItemBooks : AllDataItemBase
    {
        public const string COLUMN_BOOK_NAME = "Name";
        public const string COLUMN_BOOK_DESCRIPTION = "Description";
        public const string COLUMN_BOOK_PAGE_COUNT = "Count";
        public const string COLUMN_BOOK_PRICE = "Price";
        public const string COLUMN_BOOK_IS_ENABLE = "IsEnable";
        public const string COLUMN_BOOK_CREATE_DATE = "Date";
        public const string COLUMN_BOOK_AUTHOR = "Author";

        [AllDataItem(COLUMN_BOOK_NAME, Title=COLUMN_BOOK_NAME, Type = AllDataColumnTypes.String)]
        public string Name { get; set; }

        [AllDataItem(COLUMN_BOOK_DESCRIPTION, Title = COLUMN_BOOK_DESCRIPTION, Type = AllDataColumnTypes.Text)]
        public string Description { get; set; }

        [AllDataItem(COLUMN_BOOK_PAGE_COUNT, Title = COLUMN_BOOK_PAGE_COUNT, Type = AllDataColumnTypes.Int)]
        public int PageCount { get; set; }

        [AllDataItem(COLUMN_BOOK_PRICE, Title = COLUMN_BOOK_PRICE, Type = AllDataColumnTypes.Float)]
        public float Price { get; set; }

        [AllDataItem(COLUMN_BOOK_IS_ENABLE, Title = COLUMN_BOOK_IS_ENABLE, Type = AllDataColumnTypes.Bool)]
        public bool IsEnable { get; set; }

        [AllDataItem(COLUMN_BOOK_CREATE_DATE, Title = COLUMN_BOOK_CREATE_DATE, Type = AllDataColumnTypes.DateTime)]
        public DateTime Date { get; set; }

        [AllDataItem(COLUMN_BOOK_AUTHOR, Title = COLUMN_BOOK_AUTHOR, Type = AllDataColumnTypes.String)]
        public string Author { get; set; }
    }
}
